#!/usr/local/bin/ruby

require 'webrick'
require 'WEBrickSession'
include WEBrick

class BackEndServlet < HTTPServlet::AbstractServlet
	#~ def gen_session_id
		#~ return 'toto'
	#~ end
	
	def do_GET(req, res)
		session = Session.new(req, res)
		p session
		#~ p req.cookies
		
		#~ unless req.cookies.any? {|e| e.name == 'session_id' } then
			#~ res.cookies << Cookie.new('session_id', gen_session_id)
		#~ end
	
		TCPSocket.open ('127.0.0.1', 8093) do |s|
			backreq = req.to_s.gsub /GET \/ajax\/(GET|DELETE|PUT)/, '\1 /ajax'
			s.write backreq
			res.body = s.read
		end
		#res.body = "<HTML>hello, world.</HTML>"
		#res['Content-Type'] = "text/html"
	end
end

s = HTTPServer.new(
  :Port            => 2000,
  :DocumentRoot    => Dir::pwd
)

## mount subdirectories

s.mount("/ajax/", BackEndServlet)

trap("INT"){ s.shutdown }
s.start
